package com.qiji.video.web.controller;
import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.Admins;
import com.qiji.video.exception.ServiceException;
import com.qiji.video.service.IAdminService;

import com.qiji.video.util.Md5Util;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员模块
 */
@Api(
        tags = {"管理员模块"}
)
@RestController
@RequestMapping({"/auth/super/management"})
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @ApiOperation("查询所有管理员 包括模糊查询 传名称模糊查询，不传查询全部")
    @GetMapping("/queryAllAdmin")
    public Result queryComments(int pageNum, int pageSize, String name) throws ServiceException {
        PageInfo<Admins> admins=adminService.selectAllAdmin(pageNum,pageSize,name);
//        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
//        list.size()
        return ResultUtil.success(admins);
    }
    @ApiOperation("无参查询全部管理员")
    @GetMapping("/selectAllAdmins")
    public Result selectAllAdmins() throws  ServiceException{
        List<Admins> admins = adminService.selectAllAdmins();
        return ResultUtil.success(admins);
    }
    @ApiOperation("修改管理员信息")
    @PostMapping("/updateAdmin")
    public Result updateByPrimaryKey(Admins admins) throws ServiceException {
        adminService.updateByPrimaryKey(admins);
        return ResultUtil.success();
    }
    @ApiOperation("添加管理员")
    @PostMapping("/addAdmin")
    public Result insert(Admins admins) throws ServiceException{
        admins.setPassword(Md5Util.getMd5Key(admins.getPassword()));
        adminService.insert(admins);
        return ResultUtil.success();
    }
    @ApiOperation("删除管理员")
    @DeleteMapping("/deleteAdmin")
    public Result deleteByPrimaryKey(Integer id) throws ServiceException{
        adminService.deleteByPrimaryKey(id);
        return ResultUtil.success();
    }
    @ApiOperation("修改管理员密码")
    @PostMapping("/updatepassword")
    public Result updateByPrimaryKeySelective(Integer id,String password) throws ServiceException {
        String pwd = Md5Util.getMd5Key(password);
        adminService.updateByPrimaryKeySelective(id, pwd);
        return ResultUtil.success();
    }
}
